package com.sloth.controller;

import com.sloth.pojo.Cart;
import com.sloth.pojo.Customer;
import com.sloth.service.ProductDetailsService;
import com.sloth.service.ProductService;
import com.sloth.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.stereotype.Service;

@Controller
public class ProductDetailsController {

    @Autowired
    private ProductDetailsService productDetailsService;

    @Value("${web.upload-path}")
    private String path;

    // 根据id查询商品
    @GetMapping("product/{proId}")
    public ModelAndView getById(@PathVariable("proId") Integer proId) {

        System.out.println(path);
        ServerResponse serverResponse = productDetailsService.getProductById(proId);
        ModelAndView mav = new ModelAndView();
        mav.addObject("serverResponse", serverResponse);


        ServerResponse introduction = productDetailsService.getProduct(proId);
        mav.addObject("introduction", introduction);


        ServerResponse seckill = productDetailsService.getSeckillProduct(proId);
        mav.addObject("seckill", seckill);


        // 3. 跳转到响应页面
        mav.setViewName("product/product");
//        mav.setViewName("test/productDe");
        return mav;

    }


    // 多条件查询
    /*
    条件: 类别名称,价格范围,最新发布(时间排序),价格排序
    结果: 商品列表(商品+作者名称+列表名称)+分支
    表: product,product_kind,writer

    JS: 价格排序:0 升序,1降序
        最新发布(时间排序): 0 是    1 不按照时间排序

    var condition = "kinName=油画&minPrice=200&maxPrice=500&orderByTime=0&orderByprice=0";
    var condition = "proName=黑鲨4pro&proColor=遂空黑&proMemory=12GB+256GB";
    var condition = "proName=HUAWEI nova 9 Pro&proColor=普罗旺斯&proMemory=8GB+256GB";
     */

    // 根据名称+颜色+内存 查 id,再通过 id 查商品
    @GetMapping("product/coditions")
    public String getByConditions(String proName, String proColor, String proMemory) {

        Integer proId = productDetailsService.getByConditions(proName, proColor, proMemory);
        return "redirect:/product/" + proId;


    }

    @PostMapping("cart")
    @ResponseBody
    public ServerResponse save(Cart cart , Customer customer) {
        if (customer==null){
            return ServerResponse.saveFail("用户未登录");
        }
        cart.setCustId(customer.getCustId());
        return productDetailsService.saveCart(cart);
    }


    @PutMapping("/editor/{proId}")
    @ResponseBody
    public ServerResponse updateBlobByProId(@PathVariable("proId") Integer proId, @RequestParam String editorData) {
        return productDetailsService.saveBlob(proId, editorData);
    }

    @GetMapping("/editor/{proId}")
    @ResponseBody
    public ModelAndView updateBlobByProId(@PathVariable("proId") Integer proId) {

        ModelAndView mav = new ModelAndView();

        ServerResponse introduction = productDetailsService.getProduct(proId);
        mav.addObject("introduction", introduction);

        ServerResponse introduction2 = productDetailsService.getProduct(proId);
        mav.addObject("introduction2", introduction2);

        mav.setViewName("test/test");

        return mav;
    }

}
